<?php

namespace Admin\Controller;
use Admin\Controller\GlobalController;
class AdminController extends GlobalController
{
    //发布代码
    public function publish()
    {
        if (IS_POST) {
            $file = WEB_PATH . DIRECTORY_SEPARATOR . 'gitsync.php';
            include_once $file;
            exit('<br />done: ' . $file);
        }
        $this->display();
    }

    //权限显示列表
    public function showmenulist()
    {
        $class_id = $menuM_id = I('param.menuM_id', 0);
        $menuS_id = I('param.menuS_id', 0);
        $this->assign(array('menuM_id' => $menuM_id, 'menuS_id' => $menuS_id));
        //读取主列表
        $menuMainObj = new \Admin\Model\AdminMenuModel();
        $menuMainList = $menuMainObj->getMenuList4ClassId();
        $this->assign('menuMainList', $menuMainList);
        //子列表
        if (!empty($menuM_id)) {
            $menuSubList = $menuMainObj->getMenuList4ClassId($menuM_id);
            $this->assign('menuSubList', $menuSubList);
        }
        if (!empty($menuS_id)) {
            $class_id = $menuS_id;
        }
        //获取目录列表
        $menuList = $menuMainObj->getMenuTree4ClassId($class_id, 100);
        $this->assign('menuMList', array_shift($menuList));
        $this->assign('menuSList', $menuList);

        $this->display();
    }

    //菜单单步向上或向下排序
    public function sortmenu()
    {
        $menuMainObj = new \Admin\Model\AdminMenuModel(); //实例化模型
        $menuId = I('param.id', 0);
        $sortType = I('param.type', 'up'); //获取排序参数，如没有获取到则默认向上
        try {
            if ($sortType == 'up') {
                $sortData = $menuMainObj->sortMenuUp($menuId); //向上排序
            } else if ($sortType == 'down') {
                $sortData = $menuMainObj->sortMenuDown($menuId); //向下排序
            }
            if (!empty($sortData)) {
                $this->ajaxReturn(array(), '排序OK', 'tip_success');
            }
        } catch (\Exception $exc) {
            $this->ajaxReturn(array(), $exc->getMessage(), 'tip_error');
        }
        $this->ajaxReturn(array(), '排序失败', 'tip_error');

    }

    //删除权限
    public function delmenu()
    {
        $id = I('param.id', 0);
        if ($id < 53) $this->ajaxReturn(array(), '系统目录不能删除', 'tip_error');
        $menuMainObj = new \Admin\Model\AdminMenuModel();
        try {
            if ($menuMainObj->delMenu($id)) {
                $this->ajaxReturn(array(), '删除成功', 'tip_success');
            }
        } catch (\Exception $exc) {
            $this->ajaxReturn(array(), $exc->getMessage(), 'tip_error');
        }
        $this->ajaxReturn(array(), '删除失败', 'tip_error');
    }

    //修改目录内容
    public function savemenu()
    {
        if (IS_POST) {
            $addArray = array();
            $id = I('param.id', 0);
            $class_id = I('param.class_id', 0);
            $menuM_id = I('param.menuM_id', 0);
            $menuS_id = I('param.menuS_id', 0);
            if (!empty($menuS_id)) {
                $faClass = $menuS_id;
            } else if (!empty($menuM_id)) {
                $faClass = $menuM_id;
            } else {
                $faClass = 0;
            }
            $menuMainObj = new \Admin\Model\AdminMenuModel();
            if (empty($class_id)) {
                $class_id = $menuMainObj->getNextChild($faClass);
            }
            $addArray['name'] = I('param.name', '', 'trim');
            $addArray['id'] = $id;
            $addArray['class_id'] = $class_id;
            $newId = $menuMainObj->doSave($addArray);
            $sucMes = empty($id) ? '添加目录成功' : '修改目录成功';
            if (!empty($newId)) {
                $this->ajaxReturn(array(), $sucMes, 'tip_success');
            }
            $this->ajaxReturn(array(), '操作失败', 'tip_error');
        }

        $id = I('param.id', 0);
        $classId = I('param.id', 0);
        $menuMainObj = new \Admin\Model\AdminMenuModel();
        $menuInfo = $menuMainObj->getMenuInfo($id);
        if ($menuInfo) {
            $classId = $menuInfo['class_id'];
        }
        $menuM_id = strlen($classId) >= 2 ? substr($classId, 0, 2) : 0;
        $menuS_id = strlen($classId) >= 4 ? substr($classId, 0, 4) : 0;
        $this->assign('menuInfo', $menuInfo);
        $this->assign(array('menuM_id' => $menuM_id, 'menuS_id' => $menuS_id));

        //读取主列表
        $menuMainList = $menuMainObj->getMenuList4ClassId();
        $this->assign('menuMainList', $menuMainList);
        //子列表
        $menuSubList = $menuMainObj->getMenuList4ClassId($menuM_id);
        $this->assign('menuSubList', $menuSubList);
        $this->display();
    }

    //登陆页面
    public function login()
    {
        $this->display();
    }

    //POST登录
    public function dologin()
    {
        $userName = I('post.username', '', 'trim');
        $passWord = I('post.password', '', 'trim');
        $userAdminObj = new \Admin\Model\AdminModel();
        $userInfo = $userAdminObj->getAdminInfoByUserName($userName);
        if (is_array($userInfo) && count($userInfo) > 0) {
            if ($userInfo['password'] == $this->encryptPassWord($passWord)) {
                session('ADMIN_UID', $userInfo['id']);
                session('ADMIN_UNAME', $userInfo['username']);
                $this->ajaxReturn(array('url' => U('/admin/index')), '成功登陆！', 'success');
            }
        }
        $this->ajaxReturn(array(), '用户名或密码错误！', 'tip_error');
    }

    //退出登录
    public function logout()
    {
        session(NULL);
        redirect(U('/admin/admin/login'), 0);
    }

    //修改密码
    public function changepwd()
    {
        if (IS_POST) {
            $oldPass = I('post.oldpass', '', 'trim');
            $newPass = I('post.newpass', '', 'trim');
            $newPassAgain = I('post.newpassagain', '', 'trim');

            //验证新密码
            if ($newPass != $newPassAgain) {
                $this->ajaxReturn(array(), '两次输入新密码不一致！', 'tip_error');
            }

            //获取User信息
            $userAdminObj = new \Admin\Model\AdminModel();
            $userInfo = $userAdminObj->getAdminInfoById(session('ADMIN_UID'));

            //修改密码
            if ($userInfo['password'] == $this->encryptPassWord($oldPass)) {
                if ($userAdminObj->saveNewPassWord($userInfo['id'], $this->encryptPassWord($newPassAgain))) {
                    $this->ajaxReturn(array(), '修改密码成功', 'tip_success');
                }
                $this->ajaxReturn(array(), '修改密码失败', 'tip_error');
            } else {
                $this->ajaxReturn(array(), '原密码输入错误', 'tip_error');
            }
        }
        $this->display();
    }

    //权限显示列表
    public function showgrouplist()
    {
        $nowPage = I('get.pageNum', 1, 'intval');
        $userGroupObj = new \Admin\Model\AdminGroupModel();
        $groupList = $userGroupObj->getNewsPageList('', '', $nowPage);
        $list = $groupList['list'];
        $userRoleObj = new \Admin\Model\AdminRoleModel();
        foreach ($list as $key => $value) {
            $list[$key]['roleName'] = $userRoleObj->getRoleName4Str($value['role_list']);
        }
        $this->assign('page', $groupList['page']);
        $this->assign('list', $list);
        $this->display();
    }

    //编辑角色权限
    public function editgrouprole()
    {
        $id = I('param.id', 0);
        $userGroupObj = new \Admin\Model\AdminGroupModel();
        $roleInfo = $userGroupObj->getUserGroupInfo($id);
        $roleArray = array_flip(explode(',', $roleInfo['role_list']));
        $this->assign('roleArray', $roleArray);
        $this->assign('roleInfo', $roleInfo);
        //获取角色列表
        $userRoleObj = new \Admin\Model\AdminRoleModel();
        $roleList = $userRoleObj->getRoleAllList();
        $this->assign('roleList', $roleList);
        $this->display();
    }

    //删除角色
    public function delgroup()
    {
        $id = I('param.id', 0);
        $userGroupObj = new \Admin\Model\AdminGroupModel();
        try {
            if ($userGroupObj->delGroup($id)) {
                $this->ajaxReturn(array(), '删除成功', 'tip_success');
            }
        } catch (\Exception $exc) {
            $this->ajaxReturn(array(), $exc->getMessage(), 'tip_error');
        }
        $this->ajaxReturn(array(), '删除失败', 'tip_error');
    }

    //修改目录内容
    public function savegroup()
    {
        if (IS_POST) {
            $addArray = array();
            $id = I('param.id', 0);
            $addArray['id'] = $id;
            $name = I('post.name', '', 'trim');
            if ($name) $addArray['name'] = $name;
            $role_list = I('post.role');
            if ($role_list) $addArray['role_list'] = implode(",", $role_list);

            $userGroupObj = new \Admin\Model\AdminGroupModel();
            try {
                $newId = $userGroupObj->doSave($addArray);
            } catch (\Exception $e) {
                $this->ajaxReturn('', $e->getMessage(), 'tip_error');
            }
            $sucMes = empty($id) ? '添加用户组成功' : '修改用户组成功';
            if (!empty($newId)) {
                $this->ajaxReturn(array(), $sucMes, 'tip_success');
            }
            $this->ajaxReturn(array(), '操作失败', 'tip_error');
        }
        $id = I('param.id', 0);
        $userGroupObj = new \Admin\Model\AdminGroupModel();
        $groupInfo = $userGroupObj->getUserGroupInfo($id);
        $this->assign('groupInfo', $groupInfo);
        $this->display();
    }

    //权限显示列表
    public function showrolelist()
    {
        $nowPage = I('get.pageNum', 1, 'intval');
        $userRoleObj = new \Admin\Model\AdminRoleModel();
        $newsList = $userRoleObj->getNewsPageList('', '', $nowPage);
        $list = $newsList['list'];
        $permissionAtom = new \Admin\Model\AdminAtomModel();

        foreach ($list as $key => $value) {
            $list[$key]['atomName'] = $permissionAtom->getAtomName4Str($value['atom_list']);
        }
        $this->assign('page', $newsList['page']);
        $this->assign('list', $list);
        $this->display();
    }

    //编辑角色权限-角色信息导出json
    public function rolejsonlist()
    {
        $id = I('get.jid'); //获取角色id
        $page = I('get.page') - 1; //当前页面转换成数组下标
        $class_id = '';
        $menuSubList = '';
        $menuMainList = '';
        $searchList = '';
        $menuM_id = I('get.menuM_id');
        $menuS_id = I('get.menuS_id');
        $searchList_id = I('get.search_id');
        $nowNode = array('nodeM' => 0, 'nodeS' => 0, 'nodeBt' => 0);
        $pagesize = 10;

        $where = array();
        if (empty($page)) {
            $page = 0;
        }

        //读取主列表
        $menuMainObj = new \Admin\Model\AdminMenuModel();
        $menuMainList = $menuMainObj->getMenuList4ClassId();

        //子列表
        if (!empty($menuM_id)) {
            $menuSubList = $menuMainObj->getMenuList4ClassId($menuM_id);
            $class_id = $menuM_id;
        }
        //三级列表
        if (!empty($menuS_id)) {
            $searchList = $menuMainObj->getMenuList4ClassId($menuS_id);
            $class_id = $menuS_id;
        }
        if (!empty($searchList_id)) {
            $class_id = $searchList_id;
        }

        $atomList = $menuMainObj->getAtom4PageMenuId($class_id, $page, $pagesize, $where); //输出翻页权限色列表
        $atomListArray = array();
        foreach ($atomList['atomPageList'] as $k => $v) {
            $atomListArray[$k]['id'] = $v['id'];
            $atomListArray[$k]['title'] = $v['name'];
            $atomListArray[$k]['func_name'] = $v['func_name'];
        }
        $selectedAtom = $this->selectedAtom($id); //获取已选择的权限列表
        $datanum = $atomList['atomCount'];
        $atomJsonArray['status'] = 'success';
        $atomJsonArray['data'] = array('datanum' => $datanum, 'datalist' => $atomListArray, 'selected' => $selectedAtom, 'menuM' => $menuMainList);

        $jsonList = json_encode($atomJsonArray);
        echo $jsonList;
    }

    protected function selectedatom($id)
    { //通过角色id找出已有的权限
        if (!empty($id)) {
            $userRoleObj = new \Admin\Model\AdminRoleModel();
            $menuMainObj = new \Admin\Model\AdminMenuModel();
            $roleInfo = $userRoleObj->getUserRoleInfo($id);
            $atomArray = array_flip(explode(',', $roleInfo['atom_list']));
            $atomAllList = $menuMainObj->getAtom4MenuId(); //输出所有权限色列表
            $selectedAtom = array();
            foreach ($atomAllList as $k => $v) {
                if (isset($atomArray[$v['id']])) {
                    $selectedAtom[] = array(
                        'id' => $v['id'],
                        'title' => $v['name'],
                        'func_name' => $v['func_name']
                    );
                    //输出选中的权限id
                }
            }
            return $selectedAtom;
        }
    }

    //删除角色
    public function delrole()
    {
        $id = I('param.id', 0);
        $userRoleObj = new \Admin\Model\AdminRoleModel();
        try {
            if ($userRoleObj->delRole($id)) {
                $this->ajaxReturn(array(), '删除成功', 'tip_success');
            }
        } catch (\Exception $exc) {
            $this->ajaxReturn(array(), $exc->getMessage(), 'tip_error');
        }
        $this->ajaxReturn(array(), '删除失败', 'tip_error');
    }

    //修改角色权限
    public function saverole()
    {
        if (IS_POST) {
            $addArray = array();
            $id = I('param.id', 0);
            if ($id) $addArray['id'] = $id;
            $atomId = I('param.atomId', 0);
            if ($atomId) $addArray['atom_list'] = $atomId;
            $name = I('param.name', '');
            if ($name) $addArray['name'] = $name;

            //避免权限数量过多超过varchar（255）
            $atomIdLen = strlen($addArray['atom_list']);
            if ($atomIdLen > 255) {
                $this->ajaxReturn(array(), '已选权限数量超过最大限度，添加失败！', 'tip_error');
            }
            $userRoleObj = new \Admin\Model\AdminRoleModel();
            try {
                $newId = $userRoleObj->doSave($addArray);
            } catch (\Exception $e) {
                $this->ajaxReturn('', $e->getMessage(), 'tip_error');
            }
            $sucMes = empty($id) ? '添加角色成功' : '修改角色成功';
            if (!empty($newId)) {
                $this->ajaxReturn(array(), $sucMes, 'tip_success');
            }
            $this->ajaxReturn(array(), '操作失败', 'tip_error');
        }
        $id = I('param.id', 0);
        $userRoleObj = new \Admin\Model\AdminRoleModel();
        $roleInfo = $userRoleObj->getUserRoleInfo($id);
        $this->assign('roleInfo', $roleInfo);
        $this->display();
    }

    //权限显示列表
    public function showadminlist()
    {
        $where = array();
        $group_id = $menuM_id = I('param.groupM_id', 0);
        $group_status = I('param.groupM_status', 99);
        $this->assign(array('groupM_id' => $group_id, 'groupM_status' => $group_status));
        //创建页面搜索约束条件
        if (!empty($group_id)) {
            $where['group_id'] = $group_id;
        }
        if (isset($group_status) && $group_status != 99) {
            $where['dostatus'] = $group_status;
        }

        $nowPage = I('get.pageNum', 1, 'intval');
        $userAdminObj = new \Admin\Model\AdminModel();
        $groupList = $userAdminObj->getNewsPageList($where, '', $nowPage);
        $list = $groupList['list'];
        if (!is_array($list)) {
            $list = array();
        }
        $userGroupObj = new \Admin\Model\AdminGroupModel();
        foreach ($list as $key => $value) {
            $list[$key]['groupName'] = $userGroupObj->getGroupName4Id($value['group_id']);
        }
        //获取所有分组列表
        $groupAllList = $userGroupObj->getGroupAllList();

        $this->assign('page', $groupList['page']);
        $this->assign('list', $list);
        $this->assign('groupAllList', $groupAllList);
        $this->display();
    }

    //修改用户密码页面
    public function editadminpwd()
    {
        $id = I('param.id', 0);
        $this->assign('id', $id);
        $this->display();
    }

    //删除用户
    public function deladmin()
    {
        $id = I('param.id', 0);
        if ($id < 2) $this->ajaxReturn(array(), '最高管理员不能删除', 'tip_error');
        $userAdminObj = new \Admin\Model\AdminModel();
        try {
            if ($userAdminObj->delAdmin($id)) {
                $this->ajaxReturn(array(), '删除成功', 'tip_success');
            }
        } catch (\Exception $exc) {
            $this->ajaxReturn(array(), $exc->getMessage(), 'tip_error');
        }
        $this->ajaxReturn(array(), '删除失败', 'tip_error');
    }

    //修改用户内容
    public function saveadmin()
    {
        if (IS_POST) {
            $addArray = array();
            $id = I('param.id', 0);
            $addArray['id'] = $id;
            $password = I('param.password', '', 'trim');
            if ($password) {
                $addArray['password'] = $this->encryptPassWord($password);
            }
            $username = I('param.username', '', 'trim');
            if ($username) {
                $addArray['username'] = $username;
                $addArray['realname'] = I('param.realname');
                $addArray['group_id'] = I('param.group_id');
                $addArray['dostatus'] = I('param.dostatus');
                $addArray['content'] = I('param.content');
            }
            $userAdminObj = new \Admin\Model\AdminModel();
            try {
                $newId = $userAdminObj->doSave($addArray);
            } catch (\Exception $e) {
                $this->ajaxReturn('', $e->getMessage(), 'tip_error');
            }
            $sucMes = empty($id) ? '添加用户成功' : '修改成功';
            if (!empty($newId)) {
                $this->ajaxReturn(array(), $sucMes, 'tip_success');
            }
            $this->ajaxReturn(array(), '操作失败', 'tip_error');
        }
        $id = I('param.id', 0);
        $userAdminObj = new \Admin\Model\AdminModel();
        $adminInfo = $userAdminObj->getAdminInfoById($id);
        $this->assign('adminInfo', $adminInfo);

        $userGroupObj = new \Admin\Model\AdminGroupModel();
        $groupList = $userGroupObj->getGroupAllList();
        $this->assign('groupList', $groupList);
        $this->display();
    }

    //权限显示列表
    public function showatomlist()
    {
        $menuM_id = (String)I('param.menuM_id', 0);
        $menuS_id = (String)I('param.menuS_id', 0);
        $search_id = (String)I('param.search_id', 0);
        $class_id = $menuM_id;
        $this->assign(array('menuM_id' => $menuM_id, 'menuS_id' => $menuS_id, 'search_id' => $search_id));
        //读取主列表
        $menuMainObj = new \Admin\Model\AdminMenuModel();
        $menuMainList = $menuMainObj->getMenuList4ClassId();
        $this->assign('menuMainList', $menuMainList);

        //子列表
        if (!empty($menuM_id)) {
            $menuSubList = $menuMainObj->getMenuList4ClassId($menuM_id);
            $this->assign('menuSubList', $menuSubList);
        }
        //查询列表
        if (!empty($menuS_id)) {
            $searchList = $menuMainObj->getMenuList4ClassId($menuS_id);
            $this->assign('searchList', $searchList);
            $class_id = $menuS_id;

        }
        if (!empty($search_id)) {
            $class_id = $search_id;
        }

        //获取权限列表
        $atomList = $menuMainObj->getAtom4MenuId($class_id);
        $this->assign('atomList', $atomList);
        $this->display();
    }

    //删除权限
    public function delatom()
    {
        $id = I('param.id', 0);
        if ($id < 142) $this->ajaxReturn(array(), '系统权限不能删除', 'tip_error');

        $permissionObj = new \Admin\Model\AdminAtomModel();
        if ($permissionObj->delAtom($id)) {
            $this->ajaxReturn(array(), '删除成功', 'tip_success');
        }
        $this->ajaxReturn(array(), '删除失败', 'tip_error');
    }

    //ajax 联动取值
    public function ajaxgetatomsub()
    {
        $id = (String)I('get.id');
        if (empty($id)) {
            $this->ajaxReturn('', '', 'success');
        }
        $menuMainObj = new \Admin\Model\AdminMenuModel();
        $menuList = $menuMainObj->getMenuList4ClassId($id);
        $subArray = array();
        foreach ($menuList as $key => $value) {
            $subArray[$key]['text'] = $value['name'];
            $subArray[$key]['value'] = $value['class_id'];
        }
        $this->ajaxReturn($subArray, '', 'success');
    }

    public function saveatom()
    {
        if (IS_POST) {
            $addArray = array();
            $id = I('param.id', 0);
            $menuM_id = I('param.menuM_menuM_id', 0);
            $menuS_id = I('param.menuS_id', 0);
            $search_id = I('param.search_id', 0);
            if (!empty($search_id)) {
                $addArray['menu_main_class'] = $search_id;
            } else if (!empty($menuS_id)) {
                $addArray['menu_main_class'] = $menuS_id;
            } else if (!empty($menuM_id)) {
                $addArray['menu_main_class'] = $menuM_id;
            } else {
                $this->ajaxReturn(array(), '参数填写缺少', 'tip_error');
            }
            $permissionAtomObj = new \Admin\Model\AdminAtomModel();
            $addArray['func_data'] = I('post.func_data');
            $addArray['func_name'] = I('post.func_name', '', 'strtolower');
            $addArray['func_name'] = '/' . trim($addArray['func_name'], '/');
            $addArray['hidden_type'] = I('post.hidden_type');
            $addArray['id'] = I('post.id');
            $addArray['name'] = I('post.name');

            try {
                $newId = $permissionAtomObj->doSave($addArray);
            } catch (\Exception $e) {
                $this->ajaxReturn('', $e->getMessage(), 'tip_error');
            }
            $sucMes = empty($id) ? '添加权限成功' : '修改权限信息成功';
            if (!empty($newId)) {
                $this->ajaxReturn(array(), $sucMes, 'tip_success');
            }
            $this->ajaxReturn(array(), '操作失败', 'tip_error');
        }
        $id = I('param.id', 0);
        $permissionAtomObj = new \Admin\Model\AdminAtomModel();
        $atomInfo = $permissionAtomObj->getPermissionAtomInfo($id);
        $this->assign('atomInfo', $atomInfo);

        $search_id = $atomInfo['menu_main_class'];
        $menuM_id = strlen($search_id) >= 2 ? substr($search_id, 0, 2) : 0;
        $menuS_id = strlen($search_id) >= 4 ? substr($search_id, 0, 4) : 0;
        $this->assign(array('menuM_id' => $menuM_id, 'menuS_id' => $menuS_id, 'search_id' => $search_id));
        //读取主列表
        $menuMainObj = new \Admin\Model\AdminMenuModel();
        $menuMainList = $menuMainObj->getMenuList4ClassId();
        $this->assign('menuMainList', $menuMainList);
        //子列表
        if (!empty($menuM_id)) {
            $menuSubList = $menuMainObj->getMenuList4ClassId($menuM_id);
            $this->assign('menuSubList', $menuSubList);
        }
        //查询列表
        if (!empty($menuS_id)) {
            $searchList = $menuMainObj->getMenuList4ClassId($menuS_id);
            $this->assign('searchList', $searchList);
        }
        $this->display();
    }
}

